# 输入货币金额，转换为金额中文大写。
# 具体要求如下：
# 【1】输入的金额不能小于等于0，金额最大到仟亿单位
# 【2】如果没有小数，后面应该加一个“整"
# 【3】对于转换的结果要优化，
# 比如：壹万零仟零佰零壹圆整应该优化为：壹万零壹圆整 (难)

from tkinter import *
from tkinter.messagebox import *


class Money_transfer_GUI(Tk):
    def __init__(self):
        super().__init__()
        self.geometry("600x200+700+300")
        self.resizable(0, 0)
        self.setup_UI()

    def setup_UI(self):
        self.Label_input = Label(self, text="输入金额", font=("微软雅黑", 16))
        self.Label_input.place(x=50, y=50)
        self.Label_output = Label(self, text="金额大写", font=("微软雅黑", 16))
        self.Label_output.place(x=50, y=120)

        self.input = StringVar()
        self.Entry_input = Entry(self,
                                 textvariable=self.input,
                                 font=("微软雅黑", 16))
        self.Entry_input.place(x=180, y=50)
        self.result_output = StringVar()
        self.Label_result = Label(self,
                                  textvariable=self.result_output,
                                  font=("微软雅黑", 16),
                                  fg="red")
        self.Label_result.place(x=180, y=120)

        self.Button_convert = Button(self,
                                     text="转换",
                                     font=("微软雅黑", 16),
                                     command=self.transfer)
        self.Button_convert.place(x=450, y=50)

    def show(self):
        self.mainloop()

    def transfer(self):
        # 获取填入的金额
        input_number = self.input.get()
        #
        try:
            temp = MoneyTransfer()
            result = temp.convert(input_number)
            self.result_output.set("".join(result))
        except:
            pass


class MoneyTransfer:
    def convert(self, content):

        if self.check(content):
            # 转为两位小数
            content = str("%.2f" % eval(content))
            content = content.replace(".", "")
            # 转换为中文 列表形式
            convert_chinese = list(self.replace_chinese(content))
            #添加单位
            self.insert_unit(convert_chinese)
            # 优化
            # self.optimize_chinese("".join(self.result))
            # 输出 list转str
            return self.result

    def check(self, content):

        try:
            if content == "":
                showinfo("系统消息", "金额不能为空")
            elif type(float(content)) is float and float(
                    content) > 0 and float(content) < 1000000000000:
                return True
        except:
            showinfo("系统消息", "输入正确的金额")

    def insert_unit(self, chinese_num: list):
        self.result = []
        chinese_num.reverse()
        # 元组不可变
        unit = ("分", "角", "圆", "拾", "百", "千", "万", "拾", "百", "千", "亿", "拾",
                "百", "千")
        for i in range(len(chinese_num)):
            self.result.append(unit[i])
            self.result.append(chinese_num[i])
        return self.result.reverse()

    def replace_chinese(self, num):
        new_num = ""
        dict_convert = ("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖")
        for each in num:
            new_num += dict_convert[int(each)]
        return new_num

    # def optimize_chinese(self,money):
    #     if "零分零角" in money:
    #         money = money.replace("零角零分","整")


if __name__ == "__main__":
    this_project = Money_transfer_GUI()
    this_project.show()